﻿using Cloud.Core.Data;
using Cloud.Dto;
using Cloud.Dto.Sys;
using Cloud.Entity;
using Cloud.UnitOfWork;
using Cloud.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Cloud.Service.Module
{
    public class ModuleService : IModuleService
    {
        private  IRepository<Sys_Module> sysModuleRep;
        //public ModuleService(

        //    IRepository<Sys_Module> _modelRepository
        //    )
        //{
        //    this._modelRepository = _modelRepository;
        //}
        private readonly IUnitOfWork _unitOfWork;
        public ModuleService(IUnitOfWork unitOfWork)
        {
            this._unitOfWork = unitOfWork;
            sysModuleRep = _unitOfWork.GetRepository<Sys_Module>();
        }
        public async Task<List<ModuleDto>> GetModuleByParentId(int parentId)
        {
            var query = await sysModuleRep.LoadAsync(c => c.IsDeleted == false && c.ParentID == parentId);
            return query.OrderBy(c => c.Sort).Select(c => new ModuleDto()
            {
                ModuleId = c.Id,
                ActionName = c.ActionName,
                ControllerName = c.ControllerName,
                ModuleName = c.Name,
                ParentId = c.ParentID,
                Sort = c.Sort,
                IsParent= sysModuleRep.Entities.Where(e=>e.ParentID==c.Id&&e.IsDeleted==false).Any()
            }).ToList();
        }
        public async Task<List<ModuleDto>> GetAllModuleList()
        {
            var query = await sysModuleRep.LoadAsync(c => c.IsDeleted == false);
            return query.OrderBy(c => c.Sort).Select(c => new ModuleDto()
            {
                ModuleId = c.Id,
                ActionName = c.ActionName,
                ControllerName = c.ControllerName,
                ModuleName = c.Name,
                ParentId = c.ParentID,
                Sort = c.Sort
            }).ToList();
        }
        private int _moduleId;
        private int _parentId;
        public int ModuleId { get; set; }
        public IModuleService Init(int moduleId, int parentId)
        {
            this._moduleId = moduleId;
            this._parentId = parentId;
            return this;
        }
        public ModuleDto Info
        {
            get
            {
                var model = new ModuleDto()
                {
                    ModuleId = _moduleId,
                    ParentId = _parentId
                };
                if (model.ModuleId != 0)
                {
                    var entity = sysModuleRep.Get(model.ModuleId);
                    if (entity == null)
                    {
                        throw new Exception("要查找的对象不存在");
                    }
                    model.ModuleName = entity.Name;
                    model.Sort = entity.Sort;
                    model.ControllerName = entity.ControllerName;
                    model.ActionName = entity.ActionName;
                    model.ParentId = entity.ParentID;
                }
                var parent = sysModuleRep.Get(c => c.Id == model.ParentId);
                if (parent != null)
                {
                    model.ParentName = parent.Name;
                }
                else
                {
                    model.ParentName = "无父节点";
                }
                return model;

            }
            set
            {
                var model = value;
                if (model.ModuleId == 0)
                {
                    var entity = new Sys_Module()
                    {
                        Name = model.ModuleName,
                        CreateTime = DateTime.Now,
                        CreateId = 0,
                        ActionName = model.ActionName,
                        ControllerName = model.ControllerName,
                        ParentID = model.ParentId,
                        Sort = model.Sort
                    };
                    sysModuleRep.Insert(entity);
                    ModuleId = entity.Id;
                }
                else
                {
                    var entity = sysModuleRep.Get(model.ModuleId);
                    if (entity == null)
                    {
                        throw new Exception("要查找的对象不存在");
                    }
                    entity.Name = model.ModuleName;
                    entity.ActionName = model.ActionName;
                    entity.ControllerName = model.ControllerName;
                    entity.ParentID = model.ParentId;
                    entity.Sort = model.Sort;
                    //var updatedPropertyList = PropertyExpression<Sys_Role>.Init.Property(c => c.Name).ToList();
                    sysModuleRep.Update(entity, true, null);
                    ModuleId = entity.Id;
                }
            }
        }

        public async Task Delete()
        {
            var entity = await sysModuleRep.GetAsync(_moduleId);
            entity.IsDeleted = true;
            List<Sys_Module> entityList = new List<Sys_Module>();
            entityList.Add(entity);
            FibModuleDelete(new List<int>() { _moduleId }, entityList);
            await sysModuleRep.UpdateAsync(entityList);
        }
        //递归删除所有子节点
        private void FibModuleDelete(List<int> id, List<Sys_Module> entityList)
        {
            var list = sysModuleRep.Entities.Where(c => c.IsDeleted == false && id.Contains(c.ParentID)).ToList();
            if (list.Count > 0)
            {
                foreach (var item in list)
                {
                    item.IsDeleted = true;
                }
                entityList.AddRange(list);
                var ids = list.Select(c => c.Id).ToList();
                FibModuleDelete(ids, entityList);
            }
        }
    }
}
